package com.daona.application.facade.impl;

import com.daona.application.facade.PdfFacade;
import com.daona.application.service.PdfService;
import com.daona.infrastructure.repository.db.mapper.MetricsMapper;
import com.daona.model.constants.Constants;
import com.daona.model.dto.query.PdfDataQuery;
import com.daona.model.enums.TemplateEnum;
import com.daona.model.template.AutoCarModel;
import com.daona.model.template.BaseModel;
import com.daona.model.template.EstateModel;
import com.daona.model.template.FinanceModel;
import com.google.common.collect.Maps;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RequiredArgsConstructor
public class PdfFacadeImpl implements PdfFacade {

    private final PdfService pdfService;
    private final CacheManager cacheManager;
    private final MetricsMapper metricsMapper;

    @Override
    public void htmlToPdf(HttpServletResponse response, PdfDataQuery query) throws Exception {
        //response.setContentType("application/pdf");
        //response.setHeader("Content-Disposition", "attachment; filename="+data.getTemplateName()+".pdf");

        BaseModel baseModel = null;
        if(query.getTemplateCode() == TemplateEnum.ESTATE.code) {

            Map<String, Object> metrics = Maps.newConcurrentMap();
            metrics.put("common", Constants.CSS_DOMAIN_NAME + "css/common.css");
            baseModel = EstateModel.builder()
                    .templateName(TemplateEnum.ESTATE.getName())
                    .title(query.getTitle())
                    .imageUrl(query.getImageUrl())
                    .subTitle(query.getSubTitle())
                    .metrics(metrics)
                    .build();

        }else if(query.getTemplateCode() == TemplateEnum.AUTO_CAR.code) {
            baseModel = AutoCarModel.builder()
                    .templateName(TemplateEnum.AUTO_CAR.getName())
                    .title(query.getTitle())
                    .imageUrl(query.getImageUrl())
                    .subTitle(query.getSubTitle())
                    .val1(23.9).val2("42.8%").val3(22.7).val4("44.9%").val5(12.9).val6("18.9%").val7(23.9).val8("2.8%")
                    .build();
        }else if(query.getTemplateCode() == TemplateEnum.FINANCE.code) {
            baseModel = FinanceModel.builder()
                    .templateName(TemplateEnum.FINANCE.getName())
                    .title(query.getTitle())
                    .imageUrl(query.getImageUrl())
                    .subTitle(query.getSubTitle())
                    .val1(4.18).val2("10.49%").val3(3.97).val4("9.2%").val5(2814.87).val6("63.39%")
                    .build();
        }
        pdfService.htmlToPdf(response.getOutputStream(), baseModel);
    }
}
